package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.*;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
   @Autowired
   private ICustomerService customerService;
   @Autowired
   private IEmployeeService employeeService;
   @Autowired
   private ISystemDictionaryItemService systemDictionaryItemService;
   //客户列表
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo,Model model){
        PageInfo pageInfo = customerService.list(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("sellers",employeeService.list());
        return "customer/list";
    }
    //失败客户列表
    @RequestMapping("/failList")
    public String failList(@ModelAttribute("qo")QueryObject qo,Model model){
        PageInfo pageInfo = customerService.fialList(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("sellers",employeeService.list());
        return "/customer/fail_list";
    }
     //潜在客户列表
     @RequestMapping("/potentialList")
         public String potentialList(@ModelAttribute("qo")QueryObject qo,Model model){
         PageInfo pageInfo = customerService.potentialList(qo);
         model.addAttribute("result",pageInfo);
         model.addAttribute("sellers",employeeService.list());
         model.addAttribute("jobs",systemDictionaryItemService.selectBysySystemDictionaryId());
         model.addAttribute("sources",systemDictionaryItemService.selectSourcetBysySystemDictionaryId());
         model.addAttribute("ccts",this.systemDictionaryItemService.selectMode());
         return "/customer/potential_list";
     }
    //客户池列表
    @RequestMapping("/poolList")
    public String poolList(@ModelAttribute("qo")QueryObject qo,Model model){
        PageInfo pageInfo = customerService.poolList(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("sellers",employeeService.list());
        return "/customer/pool_list";
    }
    //正式客户列表
    @RequestMapping("/officialList")
    public String officialList(@ModelAttribute("qo")QueryObject qo,Model model){
        PageInfo pageInfo = customerService.officialList(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("sellers",employeeService.list());
        return "/customer/pool_list";
    }
    //流失客户列表
    @RequestMapping("/turnoverList")
    public String turnoverList(@ModelAttribute("qo")QueryObject qo,Model model){
        PageInfo pageInfo = customerService.turnoverList(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("sellers",employeeService.list());
        return "/customer/pool_list";
    }
    //添加，修改客户信息
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        try{
           this.customerService.saveOrUpdate(customer);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
    //修改客户状态
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(@Param("customerId")Long customerId,@Param("status")Integer status){
        try{
            System.out.println(customerId);
            System.out.println(status);
            this.customerService.updateStateById(customerId,status);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }

}
